<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .box{width:100px;height:100px;background: red;position: absolute;left:1000px;top:0;}
        .line{width:1px;height:400px;position: absolute;left:400px;background: black;top:0;}
    </style>
</head>
<body>
    <div class="box"></div>
    <div class="line"></div>
</body>
<script>
    
    const obox = document.querySelector(".box")

    let t;
    let target = 400;

    obox.onclick = function(){
        clearInterval(t);
        t = setInterval(()=>{
            // 动态计算步长：剩下距离的十分之一
            let speed = (target - obox.offsetLeft)/10;
            // 根据正负步长，决定向上或向下取整
            speed = speed < 0 ? Math.floor(speed) : Math.ceil(speed);

            if( target === obox.offsetLeft ){
                clearInterval(t);
                obox.style.left = target + "px";
            }else{
                obox.style.left = obox.offsetLeft + speed + "px";
            }
        }, 30);
    }


    


</script>
</html>